﻿/*
    Copyright 2013 Bob Moss (bobbyjmoss@gmail.com)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace OpenRAMS.Model
{
    public class BookingNotes : BookingNote
    {
        //Both are private - use provided functions only.
        private List<BookingNote> notes;
        private List<String> userNames;

        //overloaded database-related variables
        private SqlConnection dbConnection;
        private SqlDataReader dbReader;
        private SqlCommand dbCommand;
        private String dbInString;

        //custom getter for notes list
        public List<BookingNote> getNotes()
        {
            return notes;
        }

        public BookingNotes()
        {
            //db-related initialisations
            initHardCodedVals();
            try { dbInString = ConfigurationManager.ConnectionStrings["default"].ConnectionString; }
            catch (Exception e) { throw e; }  //this will cause error conditions later

            //initialise lists
            notes = new List<BookingNote>();
            userNames = new List<String>();
        }

        public int Populate(int bookingID)
        {
            //re-initialise lists
            notes = new List<BookingNote>();
            userNames = new List<String>();

            //Build query - only need a list of creators' usernames, so query has been tailored accordingly
            String queryFull = "SELECT " + attribNames[1] + " FROM " + tableName + " WHERE " + attribNames[0] + "=" + bookingID;

            //Verify the ID paramerer value is sensible
            if (bookingID < 0) { return -1; }
            

            //attempt DB connection
            try
            {
                dbConnection = new SqlConnection(dbInString);
                dbConnection.Open();
            }
            catch { return -1; }

            //create command that will run query
            dbCommand = dbConnection.CreateCommand();
            dbCommand.CommandText = queryFull;
            dbCommand.CommandType = CommandType.Text;
            dbCommand.CommandTimeout = 15;

            //Run command, and tidy-up if exceptions occur
            try { dbReader = dbCommand.ExecuteReader(); }
            catch
            {
                dbReader.Close();
                dbConnection.Close();
                return -1;
            }

            //read in all the results and add them to list of users
            while (dbReader.Read())
            {
                try
                {
                    userNames.Add(dbReader[attribNames[1]].ToString());
                }
                catch { return -1; }
            }
            
            //verify more than one result. If not, report error
            if (userNames.Count() < 1) { return -1; }

            //now we know both booking ID & their creators (primary key values)
            //...and can now use these to populate list of notes
            foreach (String user in userNames)
            {
                BookingNote newNote = new BookingNote();
                int importNote = newNote.Load(bookingID, user);
                if (importNote < 0) { return -1; }  //if failed, bail with error code
                else
                {
                    notes.Add(newNote); //if all went well, add to list
                }
            }

            //verify at least one note. If not, send error code to verify there are no notes related to this booking
            if (notes.Count() < 1) { return -1; }

            //Clean-up DB connection
            try
            {
                dbReader.Close();
                dbConnection.Close();
            }
            catch { return -1; }

            return 0;
        }
    }
}